from pprint import pprint

import pymysql

from test_cases.purchase_financial_products_flow.test_purchase_fp_flow import TestPurchaseFpFlow


class DBUtil:

    def __init__(self, host, user, password, port=3306):
        self.connect = pymysql.Connect(
            host=host,
            user=user,
            password=password,
            port=port,
            charset='utf8mb4',
            cursorclass=pymysql.cursors.DictCursor,
            database="kejiyinhangyewuguanli"

        )

    def select(self, sql):
        cursor = self.connect.cursor()
        cursor.execute(sql)
        data = cursor.fetchall()
        self.connect.commit()  # 提交事务，如果不提交，那么下次查询，查不到最新的数据
        cursor.close()
        return data

    def update(self, sql):
        """
        insert、update、delete
        :param sql:
        :return:
        """
        cursor = self.connect.cursor()
        cursor.execute(sql)
        self.connect.commit()
        cursor.close()

    def close(self):
        if self.connect != None:
            self.connect.close()


if __name__ == '__main__':
    db_util = DBUtil(host='192.168.239.129', user='root', password='123456')
    print(TestPurchaseFpFlow.fp_id)
    res = db_util.select(
        f'select * from licaicanpin_goumai'
    )
    pprint(res)
    db_util.close()
